<?php
require_once("PHPExcel.php");
require_once("PHPExcel/Reader/Excel2007.php");
require_once("PHPExcel/Reader/Excel5.php");
require_once('PHPExcel/IOFactory.php');

$objExcel = new PHPExcel();
$objProps = $objExcel->getProperties();
$objProps->setCreator('EC');
$objProps->setTitle('Template');
$objExcel->setActiveSheetIndex(0);
$objActSheet = $objExcel->getActiveSheet();
$objActSheet -> getColumnDimension(PHPExcel_Cell::stringFromColumnIndex(0)) -> setAutoSize(true);
$objActSheet->getDefaultColumnDimension()->setWidth(20);
$objActSheet->getColumnDimension("I")->setWidth(30);
$objActSheet->getStyle('I1:I999')->getAlignment()->setWrapText(TRUE)->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);;
$objActSheet->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);

$excelKey = array(
    'A' => Ec::Lang('商品编码'),
    'B' => Ec::Lang('进/出口国'),
    'C' => Ec::Lang('申诉原因'),
    'D' => Ec::Lang('申诉前'),
    'E' => Ec::Lang('申诉后'),
    'F' => Ec::Lang('申诉备注'),
    'G' => Ec::Lang('处理备注'),
    'H' => Ec::Lang('处理状态'),
    'I' => Ec::Lang('状态'),
    'J' => Ec::Lang('提交时间'),
    'K' => Ec::Lang('完成时间'),
);
$file_arr = array();
foreach ($excelData as $item){
    $file_arr[] = $item;
}
array_unshift($file_arr, $excelKey);

$i = 1;
$excelKeyNum = 1;
$row = 0;
$rowThree = false;
foreach ($file_arr as $ev){
    foreach ($excelKey as $key=>$item){
        if($i == 1){
            $objActSheet->setCellValue($key . $i ,$ev[$key]);
        }else{
            $row = count($ev['AppealReason']);
            if($rowThree){
                for ($j=0;$j<$row;$j++){
                    if($key == "A" || $key == "B"){
                        $objActSheet->setCellValue($key . ($excelKeyNum) ,$ev["ProductBarcode"]);
                        $objActSheet->getStyle($key . ($excelKeyNum))->getFill()->getStartColor()->setARGB();
                    }

                }
                if($key == "K"){
                    $excelKeyNum += $row;
                }
                if($key == "I"){
                    if($row > 1){
                        $objActSheet->mergeCells("{$key}".($excelKeyNum-1).":{$key}".(($excelKeyNum-2)+$row));
                        $objActSheet->setCellValue("{$key}".($excelKeyNum-1),"dd");
                    }else{
                        $objActSheet->setCellValue("{$key}".($excelKeyNum-1),"ddcc");
                    }
                }

                if($key == "J"){
                    if($row > 1){
                        $objActSheet->mergeCells("{$key}".($excelKeyNum-1).":{$key}".(($excelKeyNum-2)+$row));
                        $objActSheet->setCellValue("{$key}".($excelKeyNum-1),"2019/11/9");
                    }else{
                        $objActSheet->setCellValue("{$key}".($excelKeyNum-1),"2019/11/9");
                    }
                }

            }else{
                for ($j=0;$j<$row;$j++){
                    if($key == "A" || $key == "B"){
                        $objActSheet->setCellValue($key . ($j+2) ,$ev["ProductBarcode"]);
                    }

                }
                if($key == "A"){
                    $excelKeyNum += $row+1;
                }
                if($key == "I"){
                    $objActSheet->mergeCells("{$key}2:{$key}".($excelKeyNum-1));
                    $objActSheet->setCellValue($key."2","dd");
                }

                if($key == "J"){
                    $objActSheet->mergeCells("{$key}2:{$key}".($excelKeyNum-1));
                    $objActSheet->setCellValue($key."2","2019/11/9");
                }
            }
        }
    }
    $i++;
    if($i > 2){
        $rowThree = true;
    }
}

header('Pragma:public');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header("Content-Disposition:inline;filename=" . $filename . '.xlsx');
$objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
$objWriter->save('php://output');
exit;